﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace ZJUploadTool
{
    public class ZJNet
    {
        public string ZJSendMsg(string strJson,string strMethod)
        {
            string strReqResult = string.Empty;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://111.205.85.74:8090/vehicle-web-station/restapi/vehicle_station/" + strMethod);
                //httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic YWRtaW46c2VjcmV0");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept = "application/json";
                httpWebRequest.Method = "POST";
                //strJson = "{\"dsId\": \"420600001\",\"vehicleNo\": \"鄂E1F895\",\"plateColorCode\": \"1\"}";
                byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(strJson);

                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(buf, 0, buf.Length);
                stream.Close();

                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                strReqResult = reader.ReadToEnd();
                reader.Close();               
                httpWebResponse.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            return strReqResult;
        }

        public static string ZJSendMsg(string strJson, string strUrl, string strMethod)
        {
            string strReqResult = string.Empty;
            
            try
            {
                string requestUri = strUrl + strMethod;
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept = "application/json";
                httpWebRequest.Method = "POST";

                byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(strJson);

                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(buf, 0, buf.Length);
                stream.Close();

                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                strReqResult = reader.ReadToEnd();
                reader.Close();
                httpWebResponse.Close();
                LogHelper.TXT_LOG txtLog = new LogHelper.TXT_LOG(strJson, strReqResult);
                txtLog.writeTxt();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                LogHelper.ERROR_LOG errLog = new LogHelper.ERROR_LOG(ex.Message,ex.StackTrace);
                errLog.wrtieTxt();
            }
            return strReqResult;
        }
    }
}
